# -*- coding: utf-8 -*-
# @Date   : 2020/2/25
# @File   : TX_LastQuarterBadTaxCnt_ZBC.py
# @Author : zhaochen

'''eof
name:过去一季度欠税次数
code:TX_LastQuarterBadTaxCnt_ZBC
tableName:
columnName:
groups:
dependencies:TX_CQ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(table_Name):
    '''
    获取表
    :param x:
    :return:
    '''
    try:
        data = TX_CQ_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            return data if isinstance(data, list) and len(data) > 0 else [{}]
    except:
        return None


def isFloat(x):
    '''
    判断x为float类型
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float):
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True', 'NAN', 'nan',
                          'False', '-inf', '-INF', '-INFINITY', '-infinity', 'NaN', 'Nan']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def formatDate(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None


def TX_LastQuarterBadTaxCnt_ZBC():
    try:
        syptQlqs = getTable('syptQlqs')
        if syptQlqs is None:
            return u'缺失值'
        elif syptQlqs == [{}]:
            return 0
        else:
            all_sum = {}  # 去重
            flag = []
            for i in syptQlqs:
                amt = i.get('SE')
                ovdu_date_str = i.get('SSSQZ')
                # SSSQZ的时间格式为 ‘2019-03-14 00:00’
                if isFloat(amt) and formatDate(ovdu_date_str) is not None:
                    ovdu_date = formatDate(ovdu_date_str)
                    if (datetime.datetime.now() - ovdu_date).days <= 90 and float(amt) > 1000:
                        all_sum.update({str(ovdu_date_str) + str(amt): float(amt)})
                    else:
                        pass
                else:
                    flag.append(None)
            if len(flag) == len(syptQlqs):
                return u'缺失值'
            else:
                return len(all_sum)
    except:
        return u'缺失值'


result = TX_LastQuarterBadTaxCnt_ZBC()